package com.tadpole.music.view.activity.me;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.me.MyStudentAdapter;
import com.tadpole.music.bean.me.MyStudentBean;
import com.tadpole.music.bean.me.QRCodeBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.me.MyStudentIView;
import com.tadpole.music.iView.me.QRCodeIView;
import com.tadpole.music.presenter.me.MyStudentPresenter;
import com.tadpole.music.presenter.me.QRCodePresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.widget.QRCodePopup;

import java.util.List;

/**
 * 我的学生
 * Created By 孙岩
 * on 2020/9/22
 */
public class MyStudentActivity extends BaseActivity implements MyStudentIView, QRCodeIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //搜索EditText
    private EditText etStudent;
    //搜索按钮
    private TextView tvSearch;
    //邀请学生
    private ImageView ivInviteStudents;
    //刷新
    private SmartRefreshLayout rl_refresh;
    //recyclerView
    private RecyclerView recyclerView;
    //adapter
    private MyStudentAdapter myStudentAdapter;
    //我的学生
    private MyStudentPresenter myStudentPresenter;
    //获取二维码
    private QRCodePresenter qrCodePresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_student);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initViews();
        initListeners();

        myStudentPresenter = new MyStudentPresenter();
        myStudentPresenter.attachView(this);
        myStudentPresenter.getMyStudent(false);

        qrCodePresenter = new QRCodePresenter();
        qrCodePresenter.attachView(this);
    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("我的学生");
        etStudent = findViewById(R.id.etStudent);
        tvSearch = findViewById(R.id.tvSearch);
        recyclerView = findViewById(R.id.recyclerView);
        rl_refresh = findViewById(R.id.rl_refresh);
        ivInviteStudents = findViewById(R.id.ivInviteStudents);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                MyStudentActivity.this.finish();
            }
        });

        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                myStudentPresenter.getMyStudent(true);
            }
        });

        //上拉加载
        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@androidx.annotation.NonNull RefreshLayout refreshLayout) {
                myStudentPresenter.getMyStudentMore();
            }
        });

        //邀请学生
        ivInviteStudents.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(SpUtil.getInstance(MyStudentActivity.this).getString(Constant.TOKEN,"").equals("")){
                        Intent intent = new Intent(MyStudentActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }else{
                        qrCodePresenter.getQRCode();
                    }
                }else{
                    ToastUtils.show("请检查您的网路设置");
                }
            }
        });
    }

    @Override
    public void showQRCode(QRCodeBean.DataBean bean) {
        Bitmap a = Mutils.createQRCodeBitmap(bean.getErweima(), 500, 500, "UTF-8", "H", "1", Color.BLACK, Color.WHITE);
        final QRCodePopup popup = new QRCodePopup(MyStudentActivity.this,a);
        popup.setPopupWindowFullScreen(true);
        popup.showPopupWindow();
    }

    @Override
    public void show401() {
        Intent intent = new Intent(MyStudentActivity.this, LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(this).putString(Constant.TOKEN,"");
    }

    @Override
    public void showMyStudent(final List<MyStudentBean.DataBeanX.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        myStudentAdapter = new MyStudentAdapter(this,list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(myStudentAdapter);
        recyclerView.setNestedScrollingEnabled(false);


        myStudentAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    if(SpUtil.getInstance(MyStudentActivity.this).getString(Constant.TOKEN,"").equals("")){
                        Intent intent = new Intent(MyStudentActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }else{
                        Intent intent = new Intent(MyStudentActivity.this,AnswerStatisticsV2Activity.class);
                        intent.putExtra("student_id",list.get(position).getId());
                        intent.putExtra("student_avatar",list.get(position).getAvatar());
                        intent.putExtra("student_nickname",list.get(position).getNickname());
                        startActivity(intent);
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void notifyAdapter() {
        myStudentAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishRefresh();
        rl_refresh.finishLoadMore();
    }
}
